package textdiff {
	import mx.collections.ArrayCollection;
	

	/**
	 * Change a block in the old file to a different block in the new file.
	 */
	public class ChangeCommand extends EditCommand {
		
	    public function ChangeCommand(oldFileInfo:FileInfo=null, newFileInfo:FileInfo=null) {
	        super( oldFileInfo, newFileInfo );
	        command = "Change";
	        if (newFileInfo != null) {
		        newLines = newFileInfo.nextBlock();
		        newLines.reportable = true;
		    }
	        if (oldFileInfo != null) {
		    	oldLines = oldFileInfo.nextBlock();
	        	oldLines.reportable = true;
	        }
	    }

	    public override function toEditScript(): String {
	    	var s:String = ""+oldLines.fromLineNum;
	    	if (oldLines.fromLineNum < oldLines.thruLineNum) {
	    		s += "," + oldLines.thruLineNum;
	    	} 
	    	s += "c\r" + newLines.textLines + ".\r";
	    	return s;
//			var s:String = ""+oldLines.fromLineNum;
//	    	if (oldLines.fromLineNum < oldLines.thruLineNum) {
//	    		s += "," + oldLines.thruLineNum;
//	    	} 
//	    	s += "d\r.\r";
//	    	s += newLines.fromLineNum + "i\r" + newLines.textLines + ".\r";
//	    	return s;
	    }

	    public override function editText(t:Array, offset:int=0): Array {
	    	// make a copy of the input array t (using Array.slice) and wrap it in an ArrayCollection
	    	var lines:ArrayCollection = new ArrayCollection(t.slice());
	    	var startPos:int = oldLines.fromLineNum+offset;
			var endPos:int = oldLines.thruLineNum+offset;
			var replacedLinesCount:int = endPos-startPos+1;
			var pos:int;
	    	for (var i: int=0; i<newLines.lines.length; i++) {
	    		pos = startPos+i;
	    		if (pos <= endPos) {
	    			// replace line when inside the range of old lines that must be changed
		    		lines.setItemAt(newLines.lines[i], pos);
		    	}
		    	else {
		    		// insert new lines outside the range of old lines that must be changed
		    		lines.addItemAt(newLines.lines[i], pos);
		    	}
	    	}
	    	
	    	// remove any old lines still remaining (in case the old lines are replaced with less lines)
	    	while (lines.length > t.length - replacedLinesCount + newLines.lines.length) {
	    		lines.removeItemAt(pos+1);
	    	}
	    	
	    	
	    	return lines.toArray();
	    }
	    
//	    public override function editText(t:Array, offset:int=0): Array {
//	    	var lines:Array = t.slice();
//	    	
//	    	for (var i:int = oldLines.fromLineNum; i<=oldLines.thruLineNum; i++) {
//	    		if (i-oldLines.fromLineNum < newLines.lines.length && i-oldLines.fromLineNum>=0) {
//	    			lines[i+offset] = newLines.lines[i-oldLines.fromLineNum];
//	    		}
//	    		else {
//	    			lines.splice(i+offset, 1);
//	    		}
//	    	}
//	    	return lines;
//	    }

	    public override function get fromLineNum(): int { return oldLines.fromLineNum; }
	    public override function get thruLineNum(): int { return oldLines.thruLineNum; }
	}
}
